package com.shop.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.shop.entity.Coupon;
import com.shop.shop.entity.UrCn;
import com.shop.shop.mapper.CouponMapper;
import com.shop.shop.service.CommodityService;
import com.shop.shop.service.CouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.shop.service.UrCnService;
import com.shop.shop.utile.MapUtile;
import com.shop.shop.utile.PageUtile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 优惠价  服务实现类
 * </p>
 *
 * @author 小🌤
 * @since 2021-04-22
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService {
    @Autowired
    private CommodityService commodityService;

    @Autowired
    private UrCnService urCnService;

    @Override
    public Map<String, Object> listPage(MapUtile mapUtile, Integer userId) {
        Page<Coupon> page = new PageUtile<Coupon>().getPage(mapUtile);
        baseMapper.selectPage(page,null);
        List<Coupon> list = page.getRecords();
       Map<String,Object> masp=new HashMap<>();
       list.forEach(coupon -> {
            coupon.setCommodity(commodityService.getById(coupon.getCmId()));
           Boolean B= null;
           try {
               UrCn user = urCnService.getUser(new UrCn(userId,coupon.getCnId()));
            B=user==null?false:true;
           } catch (Exception e) {
               B=false;
           }
           coupon.setUserIf(B);
       });

        masp.put("list", list);
        masp.put("pages", page.getPages());
        masp.put("size", page.getSize());
        masp.put("page", page.getCurrent());
        masp.put("total", page.getTotal());
        return masp;
    }

    @Override
    public boolean saveUrCn(UrCn urCn) {
        boolean b = urCnService.save(urCn);
        return b;
    }
}
